Ext.onReady(function() {
	var editRoleId = 0;
	
	var rolePermissonForm = new Ext.FormPanel({
		labelAlign: 'right',
		region: 'center',
		autoScroll:true, 
		labelWidth: 100,
		frame: true,
		items:[],
		buttons:[
			{
				text: '保存'	,
				handler: function() {
					var permissions = rolePermissonForm.getForm().getValues();
					permissions.roleId = editRoleId;
					Ext.Ajax.request({
						method: 'post',
						params: permissions,
						url: './html/struts/PermissionAction_saveRolePermission.action',
					   	success:function(resp){
					   		var obj=Ext.util.JSON.decode(resp.responseText);
							if(obj.result == 'success') {
								rolePermissonWin.hide();
								Ext.MessageBox.alert('提示', '保存成功！', function() {
									parent.getUserObj();
								});
							}
							else {
								Ext.MessageBox.alert('出错了', '保存失败!' + obj.info);
							}
					   	}
				   	})
				}
			},
			{
				text: '关闭'	,
				handler: function() {
					rolePermissonForm.removeAll();
					rolePermissonWin.hide();
				}
			}
		]
	});
	
	var rolePermissonWin = new Ext.Window({
		id: 'rolePermissonWin',
		closeAction:'hide',
		layout: 'border',
		width: 700,
		height: 420,
		items:[rolePermissonForm]
	});	
	
	window.editPermission = function(id) {
		editRoleId = id;
		Ext.Ajax.request({
			method: 'post',
			url: './html/struts/PermissionAction_getPermissionByGroup.action',
			params: {roleId: id},
		   	success:function(resp){
		   		rolePermissonForm.removeAll();
		    	var obj=Ext.util.JSON.decode(resp.responseText);
		    	
		    	for(p in obj.MapData) {
		    		var temp_group = {
		    			xtype: 'fieldset',
						title: p,
						layout: 'hbox',
						items:[]
		    		}
		    		
		    		for(var i = 0; i < obj.MapData[p].length; i++) {
		    			var achk;
		    			if(obj.MapData[p][i].rolePermissionId != 0)
		    				achk = { boxLabel: obj.MapData[p][i].permissionName, inputValue: obj.MapData[p][i].id, name: 'permissions',xtype: 'checkbox', checked: true, margins: '0 5 0 0'};
		    			else
		    				achk = { boxLabel: obj.MapData[p][i].permissionName, inputValue: obj.MapData[p][i].id, name: 'permissions',xtype: 'checkbox', margins: '0 5 0 0'};
		    			
		    			temp_group.items.push(achk);
		    		}
		    		
		    		rolePermissonForm.add(temp_group);
		    	}
		    	
		   		rolePermissonWin.show();
		    }
		});
	}
	
	var roleResWin = new Ext.Window({
		id: 'roleResWin',
		modal: true,
		width: 300,
		height: 300,
		title: '角色资源',
		closeAction:'hide',
		applyTo:'roleResWin'
	});

	var editForm = new Ext.FormPanel({
		labelAlign: 'right',
		region: 'center',
		autoScroll:true, 
		labelWidth: 100,
		frame: true,
		xtype: 'fieldset',
		items: [
			{
				items: [
					{
						columnWidth: .01, layout: 'form',
						items: [
							{ xtype: 'hidden', name: 'id', hidden:true, hiddenLabel:true}
						]
					}
				]
			},
			{
				items: [
					{
						layout: 'form',
						items: [
							{ xtype: 'textfield', name: 'roleName', fieldLabel: '角色名称',allowBlank : false,anchor : '95%'}
						]
					},
					{
						layout: 'form',
						items: [
							{ xtype: 'textfield', name: 'describle', fieldLabel: '描述',allowBlank : false,anchor : '95%'}
						]
					}
				]
			}
		],
		buttons: [
			{
				text: '保存'	,
				handler: function() {
					if(!editForm.getForm().isValid())
						return;
					
					editForm.form.doAction('submit', {
						url: '../basic/RoleAction/save.action',
						method: 'post',
						waitTitle:'请等待',
						waitMsg: '正在提交...',
						params: '',
						success: function(form, action) {
							if(action.result.result == 'success') {
								Ext.MessageBox.alert('结果', '保存成功！');
								form.reset();
								grid.getStore().reload();
								editWin.hide();
							}
						}
					});
				}
			},
			{
				text: '取消'	,
				handler: function() {
					editForm.form.reset();
					editWin.hide();
				}
			}
		]
	});
	
	var editWin = new Ext.Window({
		title: '编辑',
		layout:'fit',
		width:450,
		height:150,
		closeAction:'hide',
		plain: true,
		layout: 'border',
		items: [editForm]
	});
	
	var searchForm = new Ext.FormPanel({
		frame: true,
		title: '查询',
		collapsible : true,
		collapsed: true,
		collapseMode:'mini',
		autoHeight:true,
		region: 'north',
		labelAlign: 'right',
		items: [
			{
				layout: 'column',
				items: [
					{
						columnWidth: .33, layout: 'form',
						items: [
							{ xtype: 'textfield', name: 'roleName', fieldLabel: '角色名称',anchor : '95%'}
						]
					},
					{
						columnWidth: .33, layout: 'form',
						items: [
							{ xtype: 'textfield', name: 'describle', fieldLabel: '描述',anchor : '95%'}
						]
					}
				]
			}
		],
		buttons: [
			new Ext.Button({
				text: '查询',
				width: 70,
				handler: function() {
					var fv = searchForm.getForm().getValues();
					ds.baseParams= fv;
					ds.load({params: {start:0, limit:20}});
				}
		}),
		new Ext.Button({
				text: '清空',
				width: 70,
				handler: function() {
					searchForm.form.reset();
					ds.baseParams= {};
					ds.load({params: {start:0, limit:20}});
				}
		}),
		new Ext.Button({
				text: '新增',
				width: 70,
				handler: function() {
					editForm.form.reset();
					editWin.show();
				}
		})
		]
	});
	
	var sm = new Ext.grid.CheckboxSelectionModel({singleSelect:true});
	var cm = new Ext.grid.ColumnModel([
		sm,
		new Ext.grid.RowNumberer(),
		{header:'角色名称', dataIndex:'roleName', sortable:true},
		{header:'描述', dataIndex:'describle', sortable:true}
	]);
	var ds = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({url: '../basic/RoleAction/search.action'}),
		remoteSort: true,
		reader: new Ext.data.JsonReader(
			{
				totalProperty: 'total',
				idProperty:'id',
				root: 'invdata'
			},
			[
				{name: 'id'},
				{name: 'roleName'},
				{name: 'describle'},
				{name: 'defaultAction'}
			]
		)
	});
	ds.load({params: {start:0, limit:20}});
	var grid = new Ext.grid.GridPanel({
		region: 'center',
		ds: ds,
		cm: cm,
		sm:sm,
		viewConfig: {
			forceFit: true
		},
		tbar: new Ext.Toolbar({
			buttons: [
				{
					text: '查询'	,
					iconCls: 'find',
					handler: function() {
						if(searchForm.collapsed)
							searchForm.expand();
						else
							searchForm.collapse();
					}
				},
				{
					text: '新增',
					iconCls: 'add',
					handler: function() {
						editForm.form.reset();
						editWin.show();
					}
				},
				{
					text: '修改'	,
					iconCls: 'edit',
					handler: function() {
						var rs = grid.getSelectionModel().getSelected();
						editForm.load({
							url: '../basic/RoleAction/getDetailInfo.action',
							params: {id: rs.data.id}
						});
						editWin.show();
					}
				},
				{
					text: '编辑资源'	,
					iconCls: 'edit',
					handler: function() {
						var rs = grid.getSelectionModel().getSelected();
						var url = 'roleresources.jsp?roleId=' + rs.data.id;
					    roleResWin.update({
					    	html: '<iframe scrolling="auto" frameborder="0" width="100%" height="100%" src="'+ url + '"></iframe>'
					    });
					    roleResWin.show();
					}
				},
				{
					text: '编辑权限'	,
					iconCls: 'edit',
					handler: function() {
						alert('编辑权限');
					}
				},
				{
					text: '删除'	,
					iconCls: 'del',
					handler: function() {
						var rs = grid.getSelectionModel().getSelected();
						Ext.MessageBox.confirm('提示', '确定删除  ' + rs.data.roleName + ' ？', function(btn) {
							if(btn != 'yes') {
								return;
							}
							Ext.Ajax.request({
								method: 'post',
								url: '../basic/RoleAction/delete.action',
								params: {id: rs.data.id},
								success:function(resp){
									var obj=Ext.util.JSON.decode(resp.responseText);
									if(obj.result == 'success') {
										grid.getStore().reload();
										Ext.MessageBox.alert('提示', '删除成功！');
									}
									else {
										Ext.MessageBox.alert('报错了！！！', '删除失败！！！' + obj.info);
									}
								}
							});
						});
					}
				}
			]
		}),
		bbar: new Ext.PagingToolbar({
			pageSize: 20,
			store: ds,
			displayInfo: true,
			displayMsg: '显示第{0}条到{1}条记录,一共{2}条',
			emptyMsg: '没有记录'
		})
	});
	
	new Ext.Viewport({
		layout: 'border',
		items:[
			searchForm,grid
		]
	});
	
});